package com.water.wfo.component.api.utils;

import com.water.wfo.component.api.entity.Request;
import com.water.wfo.component.api.filter.RequestFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * packageName com.water.wfo.component.util
 *
 * @author
 * @className AuthUtils(处以class为网)
 * @date 2025/1/17
 * @description T0D0
 */
@Slf4j
public class AuthUtils {
    public static final String EN_COMMA = ",";
    public static final String EN_SEMICOLON = ";";
    public static final String EN_QUESTION = "?";
    public static final String EN_PERIOD = ".";
    public static final String EN_COLON = ":";
    public static final String EN_BAR = "-";
    public static final String ASCII_CHANGE_LINE = "\n";
    public static final String REGEX_TWO_PERIOD = "\\.\\.";
    public static final String REGEX_BLANK = "";
    static List<String> list = new ArrayList();
    public AuthUtils(){}

    public static boolean isChecked(String actionUrl){
        return true;
    }

    public static String analyLocalIp(){
        HttpServletRequest request = (HttpServletRequest) RequestFilter.getRequestMap().get();
        String ipresult = "";
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isBlank(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }

        if (StringUtils.isBlank(ip)) {
            ip = request.getHeader("X-FORWARDED-FOR");
        }

        if (StringUtils.isNotBlank(ip) && StringUtils.contains(ip, ",")) {
            String[] a = StringUtils.split(ip, ",");

            for(int i = 0; i < a.length; ++i) {
                ipresult = a[i];
                if (StringUtils.isNotEmpty(ipresult)) {
                    break;
                }
            }
        } else if (StringUtils.isNotBlank(ip)) {
            ipresult = ip;
        }

        if (StringUtils.isEmpty(ipresult) || "unknown".equalsIgnoreCase(ipresult)) {
            ipresult = request.getHeader("X-Real-IP");
        }

        if (StringUtils.isEmpty(ipresult) || "unknown".equalsIgnoreCase(ipresult)) {
            ipresult = request.getHeader("Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ipresult) || "unknown".equalsIgnoreCase(ipresult)) {
            ipresult = request.getHeader("WL-Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ipresult) || "unknown".equalsIgnoreCase(ipresult)) {
            ipresult = request.getHeader("HTTP_CLIENT_IP");
        }

        if (StringUtils.isEmpty(ipresult) || "unknown".equalsIgnoreCase(ipresult)) {
            ipresult = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (StringUtils.isEmpty(ipresult)) {
            ipresult = request.getRemoteAddr();
        }

        return ipresult;
    }

    static {
        list.add("_TOKEN");
        list.add("TGC");
        list.add("U");
    }
}
